﻿using System;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.McMagWeb.Controllers
{
    [Authorization]
    public class CommodityLibController : BaseController
    {
        private readonly CommodityLibBLL _bll = new CommodityLibBLL();

        #region 列表

        [PermissionFilter]
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Listing(int draw, int start, int length)
        {
            int order;
            int.TryParse(Request["order[0][column]"], out order);
            var direction = Request["order[0][dir]"];
            var orderByColumns = new[] { "CommodityName", "CommodityName", "SuggestedPrice", "BrandName", "CategoryName", "Unit", "Spec" };
            var search = Request["search[value]"];
            var pageIndex = (start / length) + 1;

            var theList = _bll.GetPagingList(draw, pageIndex, length, SqlSafe(search), orderByColumns[order] + " " + direction, true);
            if (theList.data != null)
            {
                foreach (var item in theList.data)
                {
                    item.Action = "<a class='btn btn-warning'  data-toggle='modal' data-target='#myModal' href='javascript:;' onclick='CommodityStorage(" + item.Id + ",\"" + item.CommodityName.Trim() + "\"," + item.CeilingPrice + "," + item.LowerPrice + "," + item.SuggestedPrice + ",\"" + (item.IsShopCanModifyPrice ? "true" : "false") + "\");'>入库</a> ";
                }
            }

            var jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;

            return jsonResult;
        }

        #endregion

        [HttpPost]
        public JsonResult AddIntoShopCommodityLib()
        {
            var strItemIds = Request["itemIds"];
            if (string.IsNullOrEmpty(strItemIds))
            {
                return Json(false);
            }
            var opResult = true;
            var arrItemIds = strItemIds.TrimEnd(',').Split(',');
            for (var i = 0; i < arrItemIds.Length; i++)
            {
                var intItemId = int.Parse(arrItemIds[i]);
                var commodity = CommodityLibBLL.Instance.GetModel(intItemId);
                var checkModel = ShopCommodityBLL.Instance.GetModelByShopIdAndCommodityId(CurrentUser.ShopId, commodity.Id);
                if (checkModel != null)
                {
                    if (checkModel.IsDelete)
                    {
                        checkModel.SoldPrice = commodity.SuggestedPrice;
                        checkModel.SellingStatus = true;
                        checkModel.IsDelete = false;
                        opResult = ShopCommodityBLL.Instance.Update(checkModel);
                    }
                }
                else
                {
                    var model = new ShopCommodity
                    {
                        SoldPrice = commodity.SuggestedPrice,
                        AddTime = DateTime.Now,
                        CommodityLibId = intItemId,
                        Quantity = 99,
                        DisplaySort = 99,
                        IsDelete = false,
                        ShopId = CurrentUser.ShopId,
                        SellingStatus = true
                    };
                    var result = ShopCommodityBLL.Instance.Add(model);
                    if (result <= 0)
                    {
                        opResult = false;
                    }
                }
            }
            return Json(opResult);
        }

        [HttpPost]
        public ActionResult CommodityStorage(CommodityLib modelint)
        {
            var result = new Result { Status = false, Message = "入库失败" };
            if (modelint == null || modelint.Id == 0)
            {
                return Json(false);
            }
            var model = CommodityLibBLL.Instance.GetModel(modelint.Id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
            if (model.LowerPrice > 0)
            {
                if (modelint.SoldPrice < model.LowerPrice)
                {
                    result.Message = "零售价格不能小于下线价格:" + model.LowerPrice;
                    return Json(result);
                }
            }
            if (model.CeilingPrice > 0)
            {
                if (modelint.SoldPrice > model.CeilingPrice)
                {
                    result.Message = "零售价格不能大于上限价格:" + model.CeilingPrice;
                    return Json(result);
                }
            }
            if (model.IsShopCanModifyPrice == false && modelint.SoldPrice != model.SuggestedPrice)
            {
                result.Message = "此商品不可修改价格";
                return Json(result);
            }
            var checkModel = ShopCommodityBLL.Instance.GetModelByShopIdAndCommodityId(CurrentUser.ShopId, modelint.Id);
            if (checkModel != null)
            {
                checkModel.Quantity = modelint.Quantity;
                checkModel.SoldPrice = modelint.SoldPrice;
                checkModel.SellingStatus = true;
                checkModel.IsDelete = false;
                checkModel.DisplaySort = modelint.DisplaySort;
                result.Status = ShopCommodityBLL.Instance.Update(checkModel);
            }
            else
            {
                var shopCommodity = new ShopCommodity
                {
                    CommodityLibId = modelint.Id,
                    ShopId = CurrentUser.ShopId,
                    SoldPrice = modelint.SoldPrice,
                    Quantity = modelint.Quantity,
                    DisplaySort = modelint.DisplaySort,
                    AddTime = DateTime.Now,
                    SellingStatus = true,
                    IsDelete = false
                };
                var ret = ShopCommodityBLL.Instance.Add(shopCommodity);
                if (ret > 0)
                {
                    result.Status = true;
                    result.Message = "入库成功";
                }
            }
            return Json(result);
        }
    }
}